<template>
  <div class="box">
    <my-space />
    <el-tabs
      class="center"
      type="border-card"
      v-model="activeName"
      @tab-click="handleClick"
    >
      <el-tab-pane label="基本信息" name="first">
        <el-form ref="form" :model="form" label-width="200px">
          <el-form-item label="我的账户">
            <span>2064520668</span>
          </el-form-item>
          <el-form-item label="用户昵称" style="width: 50%; height: 40px">
            <el-input
              v-model="form.name"
              placeholder="30天内只能修改一次"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="绑定手机">
            <span>186****2915</span>
          </el-form-item>
          <el-form-item label="个性签名">
            <el-input
              type="textarea"
              v-model="form.words"
              style="width: 50%"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="padding: 15px" type="primary" @click="onSubmit"
              >确认修改</el-button
            >
            <el-button style="padding: 15px">取消修改</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="修改头像" name="second">
        <!-- <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <span> 支持jpg、gif、png格式图片，上传文件大小不超过2MB。</span> -->
        ​
        <!-- <el-upload
          list-type="picture-card"
          action="#"
          ref="upload"
          :auto-upload="false"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :multiple="true"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
        <button @click="add($event)">上传头像</button>
        ​ -->
        <div id="app">
          <!-- 上传文件按钮，绑定change事件 -->
          <div class="upload">
            <input type="file" id="file" @change="add($event)" />
          </div>
          <!-- 显示图片的盒子 -->
          <div class="container">
            <!-- 假如变量imgs为接口返回的图片数组，循环显示 -->
            <div v-for="(item, index) in imgs" :key="index">
              <!-- 服务端地址 + 数据库里的 icon字段 便可以显示一张图片 -->
              <img :src="'http://127.0.0.1:3000/' + item.icon" alt="" />
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="修改密码" name="third">
        <el-form ref="form" :model="form" label-width="200px">
          <el-form-item label="原密码" style="width: 50%; height: 40px">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="修改密码" style="width: 50%; height: 40px">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" style="width: 50%; height: 40px">
            <el-input v-model="form.name"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button style="padding: 15px" type="primary" @click="onSubmit"
              >确认修改</el-button
            >
            <el-button style="padding: 15px">取消修改</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import axios from "axios";
import MySpace from "./MySpace.vue";
export default {
  components: { MySpace },
  data() {
    return {
      activeName: "first",
      imgs: [],
      form: {
        name: "",
        sex: "",
        words: "",
        imageUrl: "",
      },
    };
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    onSubmit() {
      console.log("submit!");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "gif/png/image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    upLoad() {
      const fileArray = this.$refs.upload.uploadFiles;
      console.log(fileArray);
      const fd = new FormData();
      for (let i = 0; i < fileArray.length; i++) {
        fd.append("picname", fileArray[i].raw);
      }
      fd.append("data", JSON.stringify(data));
      this.$axios
        .post("/api/addBook", fd, {
          headers: { "Content-Type": "multipart/form-data" },
        })
        .then((res) => {
          console.log(res.data);
        });
    },
    getPic() {
      this.messageList = product.data.data;
      for (let i = 0; i < this.messageList.length; i++) {
        const strpic = this.messageList[i].picname.split(",");
        this.messageList[i].picname = strpic;
      }
      console.log(this.messageList);
    },

    //上传图片
    add(e) {
      //获取到第一个文件
      var file = e.target.files[0];
      //可打印看看
      console.log(e.target.files);
      //新建一个 FormData 对象
      var param = new FormData();
      // 把文件添加到 FormData对象里
      param.append("icon", file);
      //可以打印看看
      console.log(param.get("file"));
      //利用axios发送post请求

      axios
        .post("http://127.0.0.1:3000/comic/add", param, {
          headers: { "Content-Type": "multipart/form-data" },
        })
        .then((res) => {
          //调用查询方法
          this.qy();
        });
    },
    //查询
    qy() {
      axios({
        url: "http://127.0.0.1:3000/comic/all",
        method: "get",
      }).then((res) => {
        //把后端返回数据赋值给imgs
        this.imgs = res.data.data;
        console.log("this.imgs:", this.imgs);
      });
    },
    created() {
      this.qy();
    },
  },
};
</script>
<style>
.el-radio__label {
  padding: 0 10px;
}
.el-form-item__label {
  font-size: 25px;
}
.el-radio__label {
  font-size: 25px;
}
.el-input__inner {
  font-size: 15px;
}
.el-textarea__inner {
  height: 80px;
}
.el-form-item__content {
  line-height: 40px;
  font-size: 25px;
}
.el-form-item {
  margin: 25px !important;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader {
  margin: 25px 0 25px 50px !important;
}
</style>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  user-select: none;
}
ul,
ol {
  list-style: none;
}
*a {
  text-decoration: none;
  color: #000;
}
.app {
  text-align: center;
  .container {
    margin: 30px auto;
    padding: 30px;
    width: 60%;
    box-shadow: 0 0 5px rgb(190, 190, 190);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  .container img {
    width: 250px;
    height: 150px;
    object-fit: cover;
    margin: 30px;
  }
  .upload {
    margin: 30px auto;
    width: 80px;
    height: 20px;
    background-color: rgba(135, 206, 235, 0.2);
    border: 1px dashed black;
    border-radius: 5px;
    position: relative;
  }
  .upload:hover {
    background-color: rgba(135, 206, 235, 1);
  }
  .upload::before {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    content: "上传图片";
    font-size: 13px;
    text-align: center;
    font-family: "fangsong";
    line-height: 20px;
    user-select: none;
  }
  #file {
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}
.box {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  .center {
    margin: 15px 0;
  }
}
</style>